<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller{
    
    function __construct(){
        parent::__construct();
        $this->load->model('login_model');
        $this->load->model('profile_model');
    }
    
    
    public function test() {
    	$email = $this->input->post('useremail');
    	$data = $this->profile_model->show_profilebyemail($email);
    	echo json_encode($data);
    }
    
    public function getDetails() {
    	if (!$this->input->is_ajax_request()) {
    		exit('No direct script access allowed');
    	}
    	$email = $this->input->post('useremail');
    	$dataArray = $this->profile_model->show_profilebyemail($email);
    	return json_encode($dataArray);
    }
    
    public function registerUser() {
    	$email = $this->input->post('useremail');
    	$password = $this->input->post('password');
    	if($this->input->post('hiddentext') === 'userNo') {
    		$this->profile_model->createUser($email, $password);
    	}
    	else {
    		$data = array(
	    		'name' => $this->input->post('fname'),
	    		'batch' => $this->input->post('batch'),
	    		'dept' => $this->input->post('dept'),
	    		'current_company' => $this->input->post('comp'),
	    		'current_desig' => $this->input->post('desig'),
	    		'curren_loc' => $this->input->post('loc'),
	    		'mobile' => $this->input->post('mob'),
    			'email_id' => $email,
    		);
    		$this->profile_model->createMember($data, $email, $password);
    		
    	}
    }
    
    public function register() {
    	$this->load->view('register');
    }
    
	public function checkemail(){
		if (!$this->input->is_ajax_request()) {
			exit('No direct script access allowed');
		}
	    $email = $this->input->post('useremail');
        $query = $this->login_model->checkemail($email);
        if($query) echo 'userNo';
        else echo 'userOk';
    } 
    
    public function index($msg = NULL){
        // Load our view to be displayed
        // to the user
        $data['msg'] = $msg;
        $this->load->view('login_view', $data);
    }
    
    public function process(){
        // Load the model
       
        // Validate the user can login
        $result = $this->login_model->validate();
        // Now we verify the result
        if(! $result){
            // If user did not validate, then show them login page again
            $msg = '<div class="alert alert-danger">Invalid username and/or password.</div>';
            $this->index($msg);
        }else{
            // If user did validate, 
            // Send them to members area
            redirect('index');
        }        
    }
    
    public function help() {
    	$this->load->view('need_help');
    }
    
    public function retrieve() {
    	if($this->input->post('problem') == '1')
    	{
    		$this->login_model->retrievePassword();
    	}
    	else if($this->input->post('problem') == '2')
    	{
    		$this->login_model->retrieveUsername();
    	}
    	redirect('public/index');
    }
    
	public function logout(){
        $this->session->sess_destroy();
        redirect('public/index');
    }
}
